<template>
  <div>
    <commhead-2 name="收获地址"/>
    <div class="container">
      <van-address-list
         v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select="select"
      />
    </div>
  </div>
</template>

<script>
import { fetchAddress, updateAddress } from '_api'
import { getToken } from '_utils'
import Commhead2 from '../../components/Commhead2.vue'
import { Toast } from 'vant'
export default {
  components: { Commhead2 },
  data () {
    return {
      chosenAddressId: '',
      list: [
      ]
    }
  },
  created () {
    this.fetchAddress()
  },
  methods: {
    // 选中改为默认
    select (item, index) {
      if (this.$route.query.from === '/order') {
        if (confirm('确认选择吗？')) {
          this.$router.replace({
            path: '/order',
            query: {
              id: item.id
            }
          })
        }
      } else {
        const newdata = {}
        for (const key in item) {
          if (key === 'name') {
            newdata.linkMan = item[key]
          } else if (key === 'tel') {
            newdata.mobile = item[key]
          } else if (key === 'id') {
            newdata.id = Number(item[key])
          } else {
            newdata[key] = item[key]
          }
        }
        newdata.isDefault = !newdata.isDefault
        this.chosenAddressId = newdata.id
        updateAddress({
          ...newdata,
          token: getToken()
        }).then(res => {
          if (res.data.code === 0) {
            this.fetchAddress()
            Toast({
              type: 'success',
              duration: 1000,
              message: '修改成功'
            })
          }
        })
      }
    },
    // 编辑
    onEdit (note) {
      // console.log(note.id)
      this.$router.push({
        path: '/addressupdate',
        query: {
          id: note.id
        }
      })
    },
    // 添加
    onAdd () {
      this.$router.push('/addresscreate')
    },
    // 获取收货地址列表
    fetchAddress () {
      fetchAddress({
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          const list = []
          res.data.data.result.forEach(item => {
            const note = {}
            for (const key in item) {
              if (key === 'linkMan') {
                note.name = item[key]
              } else if (key === 'mobile') {
                note.tel = item[key]
              } else if (key === 'id') {
                note[key] = item[key] + ''
              } else {
                note[key] = item[key]
              }
            }
            list.push(note)
          })
          this.list = list
        }
      })
    }
  },
  watch: {
    list: function (newdata) {
      console.log(newdata)
      newdata.forEach(item => {
        if (item.isDefault) {
          this.chosenAddressId = item.id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container{
  font-size: 16px;
  padding-top: 50px;
  text-align: left;
}
</style>
